package cn.com.b2office.struts2.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.ItemCollection;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.CategoryService;
import cn.com.b2office.service.CollectionService;
import cn.com.b2office.service.ItemService;

import com.opensymphony.xwork2.ActionSupport;

public class ListMemberCollectionAction extends ActionSupport implements
		SessionAware {

	private static final long serialVersionUID = 2533463715762034522L;

	private CategoryService categoryService = null;

	private ItemService itemService = null;

	private CollectionService collectionService = null;

	private Map session = null;

	private List<Category> categories = null;

	private String categoryId = null;

	private Map<String, Item> items = null;

	private List<ItemCollection> collections = null;

	private int pageSize = 6;

	private int pageCount = 1;

	private int totalNum = 0; // 总记录数

	private int totalPageNum = 0; // 一共多少页

	public void setSession(Map session) {
		this.session = session;
	}

	public String execute() throws Exception {
		User user = (User) session.get("user");
		if (user == null)
			return "login";

		categories = categoryService.findTopLevelCategories();
		int index = -1;
		for (int i = 0; i < categories.size(); i++) {
			Category c = categories.get(i);
			if (c.getItem_clsno().toUpperCase().trim().equals("QS")) {
				index = i;
				break;
			}
		}
		if(index!=-1)
			categories.remove(index);

		pageCount = pageCount < 1 ? 1 : pageCount;
		int firstResult = (pageCount - 1) * pageSize;
		if (categoryId == null || categoryId.trim().equals("")) {
			collections = collectionService.find(user.getMemberId(),
					firstResult, pageSize);
			totalNum = collectionService.getTotalCollectionSize(user
					.getMemberId());
		} else {
			collections = collectionService.find(user.getMemberId(),
					categoryId, firstResult, pageSize);
			totalNum = collectionService.getTotalCollectionSize(user
					.getMemberId(), categoryId);
		}

		List<String> itemIdList = new ArrayList<String>();
		for (ItemCollection coll : collections) {
			itemIdList.add(coll.getItemId());
		}

		List<Item> itemList = itemService.findItemsByIterate(itemIdList);

		items = new HashMap<String, Item>();
		for (Item t : itemList) {
			items.put(t.getItem_no(), t);
		}

		totalPageNum = ((Double) Math.ceil((double) totalNum / pageSize))
				.intValue();

		return SUCCESS;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setCollectionService(CollectionService collectionService) {
		this.collectionService = collectionService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public Map<String, Item> getItems() {
		return items;
	}

	public List<ItemCollection> getCollections() {
		return collections;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}
	

}
